<?php session_start();
if(!isset($_SESSION['suser'])) {
	
	echo "<script>alert('请登陆！');location.href='/pages/Login.php'</script>";
}

?>

<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta http-equiv="Content-Style-Type" content="text/css">
        <meta http-equiv="Content-Script-Type" content="text/javascript">

        <title>邮件管理系统</title>


        <style type="text/css">
			#main {
				width:900px;
			}
			#main_nav {
				margin:10px;
				width:890px;
				line-height:24px;
			    height: 24px;
                border-bottom: 1px solid #dcdcdc;
			}
			.main_nav_tab {
				padding:0 10px;
				float:left;
				border:1px solid #dcdcdc;
			    border-bottom: 0;
				cursor:pointer;
			    margin-left: 2px;
            }
            .main_nav_tab_bg{
                background: #dcdcdc;	
            }
			.active {
				padding:0 10px;
				float:left;
                border:1px solid #dcdcdc;
                border-bottom: 0;
				cursor:pointer;
			    margin-left: 2px;
                background: #dcdcdc;
            }
			#main_body {
				width:890px;
                margin-left: 10px;
			}
            #main_body_sidebar {
            	width:200px;
            	border:1px solid #dcdcdc;
			    float: left;
            }
            #main_body_content {
            	width:675px;
            	border:1px solid #dcdcdc;
                margin-left: 10px;
                float: left;
            }
			.sidebar_item {
            	width:200px;
            	border-bottom:1px solid #dcdcdc;
                text-align: center;
                height: 22px;
                margin-top: 5px;
                cursor:pointer;
            }
            .sidebar_item_active {
            	width:200px;
            	border-bottom:1px solid #dcdcdc;
                text-align: center;
                height: 22px;
                margin-top: 5px;
                cursor:pointer;
                background: #dcdcdc;
            }
            .page_user_row {
            	width:650px;
                height: 25px;
                border-bottom:1px solid #dcdcdc;
            }
            .page_user_content {
            	width:300px;
                float: left;
                height: 22px;
                text-align: center;
                margin-top: 5px;
                border-left: 1px dashed #dcdcdc;
            }
            .page_user_selected {
            	width:200px;
                height: 25px;
                border: 1px solid #dcdcdc;
            }
            .page_user_list {
            	width:300px;
                border: 1px solid #dcdcdc;
                display:none;
            }
			.page_addr_content {
            	width:160px;
                float: left;
                height: 22px;
                text-align: center;
                margin-top: 2px;
                border-left: 1px dashed #dcdcdc;
                overflow:hidden;
            }
            .page_addr_content_small {
            	width:80px;
                float: left;
                height: 22px;
                text-align: center;
                margin-top: 2px;
                border-left: 1px dashed #dcdcdc;
                overflow:hidden;
            }
            .page_template_content {
            	width:127px;
                float: left;
                height: 22px;
                text-align: center;
                margin-top: 2px;
                border-left: 1px dashed #dcdcdc;
                overflow:hidden;
            }
            .page_smtp_content {
            	width:83px;
                float: left;
                height: 22px;
                text-align: center;
                margin-top: 2px;
                border-left: 1px dashed #dcdcdc;
                overflow:hidden;
            }
            .page_task_content {
            	width:83px;
                float: left;
                height: 22px;
                text-align: center;
                margin-top: 2px;
                border-left: 1px dashed #dcdcdc;
                overflow:hidden;
            }
        }
        </style>

        <script src="../js/jquery/jquery.js" type="text/javascript"></script>
        <script src="../js/jquery/json2.js" type="text/javascript"></script>
        <script src="../js/jquery/ajaxfileupload.js" type="text/javascript"></script>
    </head>

    <body>
        <div id='hiddenDiv' style="position:absolute;z-index:99; background-color:darkgray; Filter: Alpha(Opacity=0); border:1px solid #333333;display:none;width:100%;vertical-align:center;text-align:center">&nbsp;</div>
        <div id='hiddenContent' style="position:absolute;z-index:100; background-color:white;display:none;vertical-align:center;text-align:center;top:50%;left:50%;"></div>
        
        <h1>欢迎<?php echo $_SESSION['suser']; ?>进入邮件管理系统</h1>
        <div id="main">
            <div id="main_nav">
        		<?php include 'page_main_nav.php';?>
        	</div>
        	<div id="main_body">
        	    <div id="main_body_sidebar">
        	    </div>
        	    <div id="main_body_content">
        	    </div>
        	</div>
        	<div id="main_foot">
        	</div>
        </div>
    </body>
    <script type="text/javascript">
    $(document).ready(function(){

    	$(".main_nav_tab").hover(function(){
	        	$(this).addClass("main_nav_tab_bg");
	    	},function(){
	    		$(this).removeClass("main_nav_tab_bg");
	        }
    	);
        $(".main_nav_tab").click(function(){
            $("#main_nav div").each(function(i){
                $(this).removeClass().addClass("main_nav_tab");
            });
            $(this).removeClass().addClass("active");
            
        });
    });
    function openPage(id) {
    	$.ajax({
		   type: "POST",
		   url: "/pages/page_main_body_sidebar.php",
		   data: "id="+id,
		   success: function(data){
	  		  if (data) {
	  			  $('#main_body_sidebar').html(data);
		  		  
		  		  $(".sidebar_item").hover(function(){
			          $(this).addClass("main_nav_tab_bg");
			      	},function(){
			          $(this).removeClass("main_nav_tab_bg");
			      	}
		    	  );
		          $(".sidebar_item").click(function(){
		            $("#main_body_sidebar div").each(function(i){
		                $(this).removeClass().addClass("sidebar_item");
		            });
		            $(this).addClass("sidebar_item_active");
		            
		          });
		          $('.sidebar_item:first').click();
	  		  }
		   }
		});
    }
    function user_manage(url,param,currentId,pageSize) {
    	$.ajax({
  		   type: "POST",
  		   url: url,
  		   data: "param="+param + "&currentId=" + currentId + "&pageSize=" + pageSize,
  		   success: function(data){
  	  		  if (data) {
  	  			  $('#main_body_content').html(data);
  	  		  }
    	  
  		   }
  		});
    }
    function removeUser(id) {
    	$.ajax({
    		   type: "POST",
    		   url: "/module/module_user.php",
    		   data: "opt=del&id=" + id,
    		   success: function(data){
    	  		   $('.sidebar_item:first').click();
    		   }
    		});
    }
    function addUser() {
    	$.ajax({
    		   type: "POST",
    		   url: "/module/module_user.php",
    		   data: "opt=add&name=" + $('#add_name').val() + "&password="+$('#add_password').val(),
    		   success: function(data){
    	  		   $('.sidebar_item:first').click();
    		   }
    		});
    }
    function selectAll() {
        var checked = $('#allSelector').attr("checked");
        if (checked){
        	$("[name='checkbox']").attr("checked",true);
        } else {
        	$("[name='checkbox']").attr("checked",false);
        }
    }
    function showUsers() {
        $('.page_user_list').show(500);
    }
    function selectUser(obj,id) {
        $('.page_user_selected').html($(obj).html());
        $('.page_user_selected').attr("userId",id);
        $('.page_user_list').hide(500);
    }
    function submitPrivilege() {
        var privileges='';
    	$('input[name="checkbox"]:checked').each(function(){
    		privileges += $(this).attr("value")+',';
        });
        var userId = $('.page_user_selected').attr("userId");
        if(!userId) {
            alert("请选择用户");
        } else {
        	$.ajax({
        		   type: "POST",
        		   url: "/module/module_user.php",
        		   data: "opt=privilege&privileges=" + privileges + "&userId="+userId,
        		   success: function(data){
        		       $('.sidebar_item:first').click();
        		   }
        		});
        }
    	
    }
    function addAddr() {
        var subscripted = $("#add_subscript").attr("checked")?true:false;
        var addrClass = $("#addr_class_list option[selected]").val();
        var addr = $('#add_addr').val();
        if (!isEmail(addr)) {
            alert("邮件格式不正确！");
            return;
        }
    	$.ajax({
  		   type: "POST",
  		   url: "/module/module_addr.php",
  		   data: "opt=add&add_name=" + $('#add_name').val() + "&add_addr="+$('#add_addr').val() + "&subscripted=" + subscripted +"&addrClass=" + addrClass,
  		   success: function(data){
  	  		   $('.sidebar_item:first').click();
  		   }
  		});
    }
    function editAddr(id,name,addr,classId,subcripted) {
       //debugger;
        
        var itemId = "#page_addr_"+id;
        var originalHtml = escape($(itemId).html());
        $(itemId).html('');
        var editHtml = buildAddrItem(id,name,addr,classId,"",subcripted,originalHtml,true);

        $(itemId).html(editHtml);
        $(itemId + " select option[value=" + classId + "]").attr("selected","selected");
    }

    function saveAddr(id) {
    	var itemId = "#page_addr_"+id;
    	var name = $(itemId + " div input[name='name']").val();
    	var addr = $(itemId + " div input[name='addr_name']").val();
    	var subcripted = $(itemId + " div input[name='addr_subscript']").attr('checked')?true:false;
    	var classId = $(itemId + " select option[selected]").val();
    	var className = $(itemId + " select option[selected]").text();
        if (!isEmail(addr)) {
            alert("邮件格式不正确！");
            return;
        }
    	$.ajax({
    	   type: "POST",
    	   url: "/module/module_addr.php",
    	   data: "opt=edit&id=" + id + "&name="+name + "&addr=" + addr + "&subscripted=" + subcripted + "&classId=" + classId,
    	   success: function(data){
    		  var editHtml = buildAddrItem(id,name,addr,classId,className,subcripted,"",false);
        	  $(itemId).html(editHtml);
    	   }
    	});
    	
    }
    function buildAddrItem(id,name,addr,classId,className,subcripted,originalHtml,edit) {
    	var editedHtml = "<div class='page_addr_content'>";
    	if (edit) {
    		editedHtml += "<input type='text' name='name' value='";
    		editedHtml += name;
            editedHtml += "'/>";
    	} else {
    		editedHtml += name;
    	}
    	editedHtml += "</div>";
    	
        editedHtml += "<div class='page_addr_content'>";
        if  (edit) {
        	editedHtml += "<input type='text' name='addr_name' value='";
        	editedHtml += addr;
        	editedHtml += "'/>";
        } else {
        	editedHtml += addr;
        }
        editedHtml += "</div>";

        editedHtml += "<div class='page_addr_content'>";
        if  (edit) {
        	editedHtml += "<select>";
        	
        	<?php 
        	    require_once '../data/connect.class.php';
        	    $connect = new Connect("localhost","root","","email");
        	    $querySql="select * from admin_addr_class;";
	            $resultSet = $connect->Query($querySql);
	            foreach ($resultSet as $res) {
	    	        echo "editedHtml +=\"<option value=".$res['id'].">".$res['name']."</option>\";";
	            }
        	    
        	?>
        	editedHtml += "</select>";
        } else {
        	editedHtml += className;
        }
        editedHtml += "</div>";
        
        if (subcripted) {
        	editedHtml += "<div class='page_addr_content_small'>";
        	if (edit) {
        		editedHtml += "<input type='checkbox' name='addr_subscript' checked=true/>";
        	} else {
        		editedHtml += "是";
        	}
        	editedHtml += "</div>";
        } else {
        	editedHtml += "<div class='page_addr_content_small'>";
        	if (edit) {
        		editedHtml += "<input type='checkbox' name='addr_subscript'/>";
        	} else {
        		editedHtml += "否";
        	}
        	editedHtml += "</div>";
        }
        editedHtml += "<div class='page_addr_content_small'>";
        if (edit) {
        	
        	editedHtml += "<div onclick='saveAddr(\"";
            editedHtml += id;
            editedHtml += "\")' style='cursor:pointer;float:left;margin-left:15px'>保存</div>";
            editedHtml += "<div onclick='cancelEditAddr(\"" +id +"\",\"" + originalHtml + "\")' style='cursor:pointer;'>取消</div>";
        } else {
        	editedHtml += "<div onclick='editAddr(\"";
        	editedHtml += id;
        	editedHtml += "\",\"";
        	editedHtml += name;
        	editedHtml += "\",\"";
        	editedHtml += addr;
        	editedHtml += "\",";
        	editedHtml += subcripted;
        	editedHtml += ")' style='cursor:pointer;float:left;margin-left:15px'>编辑</div>";
        	editedHtml += "<div onclick='delAddr(\"" +id +"\")' style='cursor:pointer;'>删除</div>";
        }
        
        
        editedHtml += "</div>";

        return editedHtml;
    }
    
    function cancelEditAddr(id,originalHtml) {
       // debugger;
    	var itemId = "#page_addr_"+id;
    	var html = unescape(originalHtml);
    	$(itemId).html(html);
    }
    
    function delAddr(id) {
    	$.ajax({
            type: "POST",
            url: "/module/module_addr.php",
            data: "opt=del&id=" + id,
            success: function(data){
       	 	   $('.sidebar_item:last').click();
            }
         });
    }
    
    function addTemplate() {
        var templateName = $("#template_name").val();
        var oEditor =  FCKeditorAPI.GetInstance("newTemplate");
        var templateContent = oEditor.GetXHTML(true);
        var encodedContent = encodeURIComponent(templateContent);
        var encodedContent = encodeURIComponent(encodedContent);
    	$.ajax({
      	   type: "POST",
      	   url: "/module/module_task.php",
      	   data: "opt=createTemplate&templateName=" + templateName + "&templateContent="+encodedContent,
      	   success: function(data){
    		 $('.sidebar_item:first').click();
      	   }
      	});
        
    }
    function delTemplate(id) {
    	$.ajax({
           type: "POST",
           url: "/module/module_task.php",
           data: "opt=delTemplate&id=" + id,
           success: function(data){
      	 	   $('.sidebar_item:first').click();
           }
        });

    }
    function addSmtp() {
        var smtp_name = $('#smtp_name').val();
        var from_addr = $('#from_addr').val();
        var smtp_host = $('#smtp_host').val();
        var smtp_port = $('#smtp_port').val();
        var smtp_user_name = $('#smtp_user_name').val();
        var smtp_password = $('#smtp_password').val();
        var smtp_status = $('#smtp_status option[selected]').val();

        var paramData = "opt=addSmtp&smtp_name=" + smtp_name + "&from_addr=" + 
                 from_addr + "&smtp_host=" + smtp_host + "&smtp_port=" + smtp_port + "&smtp_user_name=" + 
                 smtp_user_name + "&smtp_password=" + smtp_password + "&smtp_status=" + smtp_status;

        $.ajax({
            type: "POST",
            url: "/module/module_smtp.php",
            data: paramData,
            success: function(data){
       	 	   $('.sidebar_item:first').click();
            }
         });
    }
    
    function delSmtp(id) {
    	$.ajax({
            type: "POST",
            url: "/module/module_smtp.php",
            data: "opt=delSmtp&id=" + id,
            success: function(data){
       	 	   $('.sidebar_item:first').click();
            }
         });
    }
    function editSmtp(id,name,from_addr,host,port,user_name,password,status) {
    	hidelayer();
    	var content = "";
		content += "<label>名字:</label>"; 
		content += "<input type='text' id='updated_smtp_name' value='" + name + "' /><br/>"; 
		content += "<label>邮件地址:</label>"; 
		content += "<input type='text' id='updated_from_addr' value='" + from_addr + "' /><br/>"; 
		content += "<label>主机地址:</label>"; 
		content += "<input type='text' id='updated_smtp_host' value='" + host + "' /><br/>"; 
		content += "<label>端口:</label>"; 
		content += "<input type='text' id='updated_smtp_port' value='" + port + "' /><br/>"; 
		content += "<label>用户名:</label>"; 
		content += "<input type='text' id='updated_smtp_user_name' value='" + user_name + "' /><br/>"; 
		content += "<label>密码:</label>"; 
		content += "<input type='text' id='updated_smtp_password' value='" + password + "' /><br/>"; 
		content += "<label>状态:</label>"; 
        content += "<select id='updated_smtp_status'>";
        if (status) {
        	content += "<option value='1' selected='selected'>激活</option>";
            content += "<option value='0'>禁用</option>";
        } else {
        	content += "<option value='1'>激活</option>";
            content += "<option value='0' selected='selected'>禁用</option>";
        }
        
        content += "</select><br/>";
		
		content += "<div onclick='updateSmtp(" + id + ")' style='cursor:pointer;'>确定</div>";
		$("#hiddenContent").html(content);
    }

    function updateSmtp(id) {
        var id = id;
        var name = $("#updated_smtp_name").val();
        var from_addr = $("#updated_from_addr").val();
        var host = $("#updated_smtp_host").val();
        var port = $("#updated_smtp_port").val();
        var user_name = $("#updated_smtp_user_name").val();
        var password = $("#updated_smtp_password").val();
        var status = $("#updated_smtp_status option[selected]").val();
        
        var paramData = "opt=updateSmtp&id=" + id + "&smtp_name=" + name + "&from_addr=" + 
        from_addr + "&smtp_host=" + host + "&smtp_port=" + port + "&smtp_user_name=" + 
        user_name + "&smtp_password=" + password + "&smtp_status=" + status;

        $.ajax({
            type: "POST",
            url: "/module/module_smtp.php",
            data: paramData,
            success: function(data){
        	   displaylayer();
        	   
       	 	   $('.sidebar_item:last').click();
            }
         });
        
    }
    function hidelayer()
    {
        $('#hiddenDiv').css('display','block');
        $('#hiddenDiv').css('height','100%');
        $('#hiddenDiv').css('filter','Alpha(Opacity=40)');
        $('#hiddenDiv').css('opacity','0.4');
        $('#hiddenDiv').css('top','0');
        $('#hiddenDiv').css('left','0');
        $('body').css('overflow','hidden');
        $('#hiddenDiv').blur(); 
    	$("#hiddenContent").css("display","block");
    	$("#hiddenContent").css("width","400px");
    	$("#hiddenContent").css("height","300px");
    	$("#hiddenContent").css("margin-left","-200px");
    	$("#hiddenContent").css("margin-top","-150px");
        
    }
    function displaylayer()
    {
    	$('#hiddenDiv').css('display','none');
        $('#hiddenDiv').css('filter','Alpha(Opacity=0)');
        $('#hiddenDiv').css('opacity','0');
        $("#hiddenContent").html("");
        $("#hiddenContent").css("display","none");
    }
    
    function isEmail(str) {
        var myReg = /^[-_A-Za-z0-9]+@([_A-Za-z0-9]+.)+[A-Za-z0-9]{2,3}$/;
        if(myReg.test(str)) return true;
        return false;
    }

    function addTask() {
        var task_name = $("#task_name").val();
    	var template_id = $("#template_list option[selected]").val();

        var classArray = new Array();
    	$('input[name="classCheckbox"]:checked').each(function(){
    		classArray.push($(this).val());
        });
        var classes = classArray.toString();

        var domainArray = new Array();
    	$('input[name="domainCheckbox"]:checked').each(function(){
    		domainArray.push("'" + $(this).val() + "'");
        });
        var domains = domainArray.toString();
        
    	var addrs = new Array();
    	$('#selectDest option').each(function(){
    		addrs.push($(this).val());
        });

    	var addrIds = addrs.toString();
    	
    	var reply_name = $("#reply_name").val();
    	var reply_addr = $("#reply_addr").val();
    	if (!isEmail(reply_addr)) {
            alert("邮件格式不正确！");
            return;
    	}
//    	var smtp_id = $("#smtp_list option[selected]").val();

//        var paramData = "opt=addTask&task_name=" + task_name + "&template_id=" + template_id + "&addrs=" + addrIds + "&reply_name=" + 
//        reply_name + "&reply_addr=" + reply_addr + "&smtp_id=" + smtp_id + "&classes=" + classes + "&domains=" + domains;

        var paramData = "opt=addTask&task_name=" + task_name + "&template_id=" + template_id + "&addrs=" + addrIds + "&reply_name=" + 
        reply_name + "&reply_addr=" + reply_addr + "&classes=" + classes + "&domains=" + domains;
        
        $.ajax({
            type: "POST",
            url: "/module/module_task.php",
            data: paramData,
            success: function(data){        	   
       	 	   $('.sidebar_item:last').click();
            }
         });
    	
    }

    function runTask(id) {
        $.ajax({
            type: "POST",
            url: "/module/module_task.php",
            data: "opt=sendTask&id=" + id,
            success: function(data){        	   
       	 	   alert(data);
            }
        });
    }
    
    function ajaxFileUpload(param)
	{
		$("#loading")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
		});

		$.ajaxFileUpload
		(
			{
				url:'/module/module_import.php?type=' + param,
				secureuri:false,
				fileElementId:'fileToUpload',
				dataType: 'json',
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert(data.error);
						}else
						{
							$("#uploadInfo").html("上传成功：" + data.msg);
							$("#fileToUpload").attr("value","");
						}
					}
					
				},
				error: function (data, status, e)
				{
					alert("上传失败" + data.error + e);
				}
			}
		)
		
		return false;

	}
    function addAddrPager(url,param,currentId,pageSize) {
    	$.ajax({
  		   type: "POST",
  		   url: url,
  		   data: "param="+param + "&currentId=" + currentId + "&pageSize=" + pageSize,
  		   success: function(data){
  	  		  if (data) {
  	  			  $('#userSource').html(data);
  	  		  }
    	  
  		   }
  		});
    }
    function addAddrToSelector() {
    	$('#selectSource option:selected').clone().appendTo('#selectDest');
    }
    function removeAddrFromSelector() {
    	$('#selectDest option:selected').remove();
    }
    </script>
</html>